package com.easy.dfc.web;

import com.alibaba.fastjson.JSONObject;
import com.easy.core.web.BaseController;
import com.easy.dfc.Utils.JsonUtil;
import com.easy.dfc.Utils.PagingUtil;
import com.easy.dfc.Utils.Util;
import com.easy.dfc.Utils.Error;

import com.easy.dfc.entity.*;
import com.easy.dfc.service.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by VULCANTSeries on 2017/5/15.
 */
@Controller
@RequestMapping("${apiPath}/follow")
public class FollowController extends BaseController {

    @Resource
    private UsersetService usersetService;

    @Resource
    private FollowProductService followProductService;

    @Resource
    private FollowStoreService followStoreService;

    @Resource
    private TiantiantejiaService tiantiantejiaService;

    @Resource
    private TiaoweihuosetService tiaoweihuosetService;

    @Resource
    private TuangouService tuangouService;

    /**
     * 关注宝贝
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/AddFollowProduct", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> AddFollowProduct(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = request.getParameter("UserId");
            String token = request.getParameter("Token");
            if (!usersetService.VerifyToken(userId, token)) {
                jsonObject.put("Err", Error.ValidFailed);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            userId = Util.ToUuidString(userId);
            String ProductId = Util.ToUuidString(request.getParameter("ProductId").replaceAll("-", ""));
            Followproduct followproduct = new Followproduct();
            followproduct.setProductid(ProductId);
            followproduct.setUserid(userId);
            //已关注则不能再次关注
            Followproduct follow = followProductService.selectFollow(followproduct);
            if (follow != null) {
                jsonObject.put("Err", Error.IsFollow);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            followProductService.insertSelective(followproduct);
            jsonObject.put("Err", Error.OK);
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 取消关注宝贝
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/DelFollowProduct", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> applyTiaoweihuo(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = request.getParameter("UserId");
            String token = request.getParameter("Token");
            if (!usersetService.VerifyToken(userId, token)) {
                jsonObject.put("Err", Error.ValidFailed);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            userId = Util.ToUuidString(userId);
            //先根据userId和productId删除该条记录
            String ProductId = Util.ToUuidString(request.getParameter("ProductId").replaceAll("-", ""));
            Followproduct followproduct = new Followproduct();
            followproduct.setProductid(ProductId);
            followproduct.setUserid(userId);
            followProductService.deleteOne(followproduct);
            jsonObject.put("Err", Error.OK);
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 判断是否关注宝贝
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/DecideFollowProduct", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> DecideFollowProduct(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = request.getParameter("UserId");
            String token = request.getParameter("Token");
            if (!usersetService.VerifyToken(userId, token)) {
                jsonObject.put("Err", Error.ValidFailed);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            userId = Util.ToUuidString(userId);
            String ProductId = Util.ToUuidString(request.getParameter("ProductId").replaceAll("-", ""));
            Followproduct followproduct = new Followproduct();
            followproduct.setProductid(ProductId);
            followproduct.setUserid(userId);
            if (followProductService.selectFollow(followproduct) != null) {
                jsonObject.put("Msg", true);
            } else {
                jsonObject.put("Msg", false);
            }
            jsonObject.put("Err", Error.OK);
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * * 获取收藏宝贝列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/FollowProductList", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> FollowProductList(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = request.getParameter("UserId");
            String token = request.getParameter("Token");
            if (!usersetService.VerifyToken(userId, token)) {
                jsonObject.put("Err", Error.ValidFailed);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            userId = Util.ToUuidString(userId);
            int start = Integer.parseInt(request.getParameter("Page"));
            PagingUtil pagingUtil = new PagingUtil();
            pagingUtil.setUserId(userId);
            pagingUtil.setStart((start - 1) * 20);
            List<ProductsetWithBLOBs> productsetWithBLOBsList = followProductService.selectFollowProduct(pagingUtil);
            //遍历每条数据，判断是否在活动中
            for (ProductsetWithBLOBs p : productsetWithBLOBsList) {
                if (p != null)
                    Util.PromotionStuts(p, tiantiantejiaService, tiaoweihuosetService, tuangouService);
            }
            String str = JSONObject.toJSONString(productsetWithBLOBsList);
            jsonObject = JSONObject.parseObject(JsonUtil.toErrJson(Error.OK, str));
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 关注店铺
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/AddFollowStore", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<JSONObject> doAddFollowStore(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        Followstore followstore = new Followstore();
        try {
            String userId = request.getParameter("UserId");
            String token = request.getParameter("Token");
            if (!usersetService.VerifyToken(userId, token)) {
                jsonObject.put("Err", Error.ValidFailed);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            userId = Util.ToUuidString(userId.replaceAll("-", ""));
            String StoreId = request.getParameter("StoreId");

            followstore.setUserid(userId);
            followstore.setStoreid(StoreId);
            Followstore follow = followStoreService.selectFollow(followstore);
            if (follow != null) {
                jsonObject.put("Err", Error.IsFollow);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            followStoreService.insertSelective(followstore);
            jsonObject.put("Err", Error.OK);
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 取消关注店铺
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/DelFollowStore", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> DelFollowStore(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = request.getParameter("UserId");
            String token = request.getParameter("Token");
            if (!usersetService.VerifyToken(userId, token)) {
                jsonObject.put("Err", Error.ValidFailed);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            userId = Util.ToUuidString(userId);
            String StoreId = request.getParameter("StoreId");
            Followstore followstore = new Followstore();
            followstore.setUserid(userId);
            followstore.setStoreid(StoreId);
            followStoreService.deleteByStore(followstore);
            jsonObject.put("Err", Error.OK);
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 判断是否关注店铺
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/DecideFollowStore", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> DecideFollowStore(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = request.getParameter("UserId");
            String token = request.getParameter("Token");
            if (!usersetService.VerifyToken(userId, token)) {
                jsonObject.put("Err", Error.ValidFailed);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            userId = Util.ToUuidString(userId);
            String StoreId = request.getParameter("StoreId");
            Followstore followstore = new Followstore();
            followstore.setUserid(userId);
            followstore.setStoreid(StoreId);
            if (followStoreService.selectFollow(followstore) != null) {
                jsonObject.put("Msg", true);
            } else {
                jsonObject.put("Msg", false);
            }
            jsonObject.put("Err", Error.OK);
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }

    /**
     * 获取关注店铺列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/FollowStoreList", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> FollowStoreList(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = request.getParameter("UserId");
            String token = request.getParameter("Token");
            if (!usersetService.VerifyToken(userId, token)) {
                jsonObject.put("Err", Error.ValidFailed);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            userId = Util.ToUuidString(userId);
            List<FollowStoreList> followStoreList = followStoreService.selectFollowList(userId);
            String str = JSONObject.toJSONString(followStoreList);
            jsonObject = JSONObject.parseObject(JsonUtil.toErrJson(Error.OK, str));
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }
}
